import { defineStore } from 'pinia'
// import http from '@/utils/http'
const paths = {
  cates: 'cate/tree',
  regions: 'city/tree',
  citys: 'city/tree?level=2',
  perms: 'perm/tree',
  menus: 'menu/tree'
}
/* 商品添加 */
export const useCacheStore = defineStore('cache', {
	state: () => ({
		data: {
			cates: [] as object [],
			regions: [] as object [],
			citys: [] as object [],
			perms: [] as object [],
			menus: [] as object []
		},
		loading: false
	}),
	getters: {
		cates(): object[] {
			return this.fetch('cates')
		},
		regions(): object[] {
			return this.fetch('regions')
		},
		citys(): object[] {
			return this.fetch('citys')
		},
		perms(): object[] {
			return this.fetch('perms')
		},
		menus(): object[] {
			return this.fetch('menus')
		}
	},
	actions: {
		fetch(attr: string): object[] {
			if(this.data[attr].length == 0){
				this.loading = true
				useGet(paths[attr]).then((data: any) => {
					this.loading = false
          this.data[attr] = data
				})
			}
			return this.data[attr]
		}
	}
})